home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / WANDR401.ZIP / sources / M.c < prev    next >
C/C++ Source or Header  |  1997-11-09  |  8KB  |  339 lines

  1. /*               m.c                       */
  2.  
  3. /*     This file contains the main program */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "wand_head.h"
  8. #include <sys/time.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <allegro.h>
  13. #include "wandat.h"
  14.  
  15.  
  16.  
  17. /* This structure will contain all the bitmaps and audio samples
  18.    used by this game. The array is allocated and loaded by the allegro
  19.    function load_datafile("wandspr.dat").
  20.  */
  21.  
  22. extern char *playscreen();
  23. extern int rscreen();
  24. extern void map();
  25. int err;
  26. int debug_disp = 1;
  27. char lscreen[NOOFROWS][ROWLEN+1];
  28. int edit_mode = 0;
  29. int saved_game = 0;
  30. char *edit_screen = (char *) 0;
  31. char *edit_memory = (char *) 0;
  32. char *memory_end = (char *) 0;
  33. char screen_name[61] ;
  34. long dictsize = 0L;
  35. int old_score=0;
  36. int pause1 = 30; /* delay time for moving objects */
  37. int pause2 = 80; /* delay time for playback */
  38. int recording=0; /* need to link it also with fall.c */
  39. int levelnum=1;
  40. int leveldir = 1;
  41. int gamelevel = 1;
  42. int spritebase = 0;
  43. int audio_flag = 1;
  44. int midi_flag = 1;
  45. int midi_playing =0;
  46. int vga = 1;
  47. char  *screenpath = "./screens";
  48.  
  49. int * newwin();
  50.  
  51. int readline(fd, ptr, max)
  52. int fd, max;
  53. char *ptr;
  54. {
  55.     int count = 0;
  56.     while (read(fd,ptr,1) == 1) {
  57.     if (++count == max)
  58.         break;
  59.     if (*ptr=='\n')
  60.         break;
  61.     ptr++;
  62.     }
  63.     *ptr = '\0';
  64.     return count;
  65. }
  66.  
  67.  
  68. first_time()
  69. {
  70. char ans;
  71. printf("The game is designed to run in SVGA mode (640 x 480 x 8).\n");
  72. printf("Some video cards may not be able to run properly in SVGA mode\n");
  73. printf("in this game. If it is unable to switch to SVGA mode, then this\n");
  74. printf("game will revert to VGA mode (320 x 240 x 8) which is supported\n");
  75. printf("by all graphics boards. However, in some systems the game may\n");
  76. printf("crash on switching to SVGA mode. If this happens, then you should\n");
  77. printf("start the game in VGA mode. When the program exits normally, it\n");
  78. printf("create a file contained winallg.ini which indicates what mode\n");
  79. printf("your system supports (see README files for details).\n\n");
  80. printf("Do you wish to try the SVGA mode? Type y or n and press enter key:");
  81. fflush(stdout);
  82. scanf("%c",&ans);
  83. rest(100);
  84. if(ans == 'y') vga=1;
  85. else vga=2;
  86. }
  87.  
  88.  
  89.  
  90. /* read or write wandallg.ini which contains
  91.    initialization parameters.
  92. */
  93.  
  94. char *tokens[] = {"level", "sprite", "audio", "pause1", "pause2"
  95.                   ,"directory", "midi", "vga"};
  96.  
  97.  
  98. write_ini()
  99. {
  100. FILE *handle;
  101. handle = fopen("wandallg.ini","wt");
  102. fprintf(handle,"%s=%d\n",tokens[0],gamelevel);
  103. fprintf(handle,"%s=%d\n",tokens[1],spritebase);
  104. fprintf(handle,"%s=%d\n",tokens[2],audio_flag);
  105. fprintf(handle,"%s=%d\n",tokens[3],pause1);
  106. fprintf(handle,"%s=%d\n",tokens[4],pause2);
  107. fprintf(handle,"%s=%d\n",tokens[5],leveldir);
  108. fprintf(handle,"%s=%d\n",tokens[6],midi_flag);
  109. fprintf(handle,"%s=%d\n",tokens[7],vga);
  110. fclose(handle);
  111. }
  112.  
  113. read_ini()
  114. {
  115. FILE *handle;
  116. char buffer[100];
  117. char *param,*value,*ret;
  118. int i;
  119. if(exists("wandallg.ini"))
  120.   {
  121.   handle = fopen("wandallg.ini","rt");
  122.   ret = buffer;
  123.   do
  124.    {
  125.     ret = fgets(buffer,100,handle);
  126.     if(!ret) break;
  127.     param = strtok(buffer,"=");
  128.     value = strtok(0," \n");
  129.     for (i=0;i<7;i++)
  130.       if(!strcmp(tokens[i],param)) break;
  131.     switch (i)
  132.      {
  133.      case 0:
  134.      gamelevel = atoi(value);
  135.      levelnum = gamelevel;
  136.      break;
  137.  
  138.      case 1:
  139.      spritebase = atoi(value);
  140.      break;
  141.  
  142.      case 2:
  143.      audio_flag = atoi(value);
  144.      break;
  145.  
  146.      case 3:
  147.      pause1 = atoi(value);
  148.      break;
  149.  
  150.      case 4:
  151.      pause2 = atoi(value);
  152.      break;
  153.  
  154.      case 5:
  155.      leveldir = atoi(value);
  156.      break;
  157.  
  158.      case 6:
  159.      midi_flag = atoi(value);
  160.      break;
  161.  
  162.      case 7:
  163.      vga = atoi(value);
  164.      if(vga>2) vga=2;
  165.      if(vga<1) vga=1;
  166.      break;
  167.  
  168.      default:
  169.      break;
  170.      }
  171.     } while (ret);
  172.  
  173.   fclose(handle);
  174.   change_spritebase();
  175.   change_leveldir();
  176.   }
  177. else first_time();
  178. }
  179.  
  180.  
  181.  
  182. /*          MAIN      PROGRAM            */
  183.  
  184.  
  185. main(argc, argv)
  186. int  argc;
  187. char **argv;
  188. {
  189.     char (*frow)[ROWLEN+1] = lscreen;
  190.     long score = 0;
  191.     int fp,  maxmoves = 0, x, y;
  192.     char howdead[25], buffer[100], *name, *keys, *dead;
  193.     char c, *malloc();
  194.     struct stat statbuf;
  195.     int ier;
  196.     extern MIDI *music;
  197.  
  198.     keys = (char *) 0;
  199.     memory_end = edit_memory = malloc(EMSIZE * sizeof(char));
  200.     while ((c = getopt(argc,argv,"01k:et:r:fmCcvs")) != -1)
  201.     switch (c) {
  202.     case 'v':
  203.          printf("\nWANDERER Copyright (C) 1988 S. Shipway. Version %s.\n\n",VERSION);
  204.       printf("Adapted for MSDOS and ALLEGRO package (using DJGPP).\n");
  205.          printf("by seymour.shlien@crc.doc.ca\n");
  206.       exit (0);
  207.  
  208.  
  209.     default:
  210.         fprintf(stderr,"Usage: %s [ -v | -0 | -1 ] [ file ]\n",argv[0]);
  211.         exit(1);
  212.     }
  213.  
  214.  
  215.  
  216.     name = "noname";
  217.  
  218.     if (!keys)
  219.     if ((keys = (char *)getenv("NEWKEYS")) == NULL) {
  220.         keys = malloc(5);
  221.          strcpy(keys,"kjhl");        
  222.     }
  223.  
  224.  
  225.  
  226.    read_ini();
  227.    read_midis();
  228.  
  229.  
  230.       /* Initialise Allegro Units */
  231.   allegro_init ();
  232.   install_timer();
  233.   ier = install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
  234.   if (ier)
  235.      {
  236.      printf("%s\n\n",allegro_error);
  237.      printf("An error occurred while attempting to install_sound.\n");
  238.      printf("\n\nPress any key to continue.\n");
  239.      getch();
  240.      }
  241.   set_volume(255, 255);
  242.  
  243.   ier = set_gfx_mode (GFX_MODEX, 320, 240, 0, 0);
  244.  
  245.   if(exists("cover.mid"))
  246.    {
  247.    music = load_midi("cover.mid");
  248.    play_midi(music,0);
  249.    }
  250.  
  251.   if(!exists("cover.pcx"))
  252.     {
  253.     printf("could not find file cover.pcx\n");
  254.     getch();
  255.     }
  256.   else show_cover();
  257.  
  258.  
  259.  
  260.   play_midi(NULL,0);
  261.   if(music) destroy_midi(music);
  262.  
  263.     /* Create Main Screen */
  264.   if(vga == 1)
  265.    {
  266.    ier = set_gfx_mode (GFX_AUTODETECT, 640, 480, 0, 0);
  267.    if(ier)
  268.     {
  269.     set_gfx_mode(GFX_TEXT,640,480,0,0);
  270.      printf("%s\n\n",allegro_error);
  271.     printf("The program was unable to switch to SVGA mode using your\n");
  272.     printf("video adapter driver and running in MSDOS. It requires a\n");
  273.     printf("screen 640 by 480 by 8 bits. A UniVBE drive may fix this\n");
  274.     printf("problem.\n");
  275.     printf("\n\nPress any key to continue in VGA mode.\n");
  276.     getch();
  277.     vga = 2;
  278.     ier = set_gfx_mode (GFX_MODEX, 320, 240, 0, 0);
  279.     }
  280.    }
  281.  
  282.   create_bitmaps();
  283.   load_datafiles();
  284.  
  285.  
  286.  
  287. /* MAIN PROGRAM HERE */
  288. /* The main loop reads in screen (levelnum) and runs the
  289.    playscreen game. The function playscreen returns the game
  290.    status via the character array dead. There are 4 possible
  291.    outcomes.
  292.    1) The player completes the level and is ready for the
  293.       next level.
  294.    2) The player dies and may wish to retry the same level
  295.       or quit.
  296.    3) The player wishes to quit.
  297.    4) The player wishes to restart the same level or start
  298.       a different level.
  299.    These outcomes are signalled by the contents of the char
  300.    array, dead. If dead is NULL, then we restart with which
  301.    ever level is specified in int variable levelnum. If dead is
  302.    not null, then if dead[0] = '~', the level to be done
  303.    next is specified; otherwise the player was killed and
  304.    dead[] contains the postmortem.
  305. */
  306.         recording = 1; /* always start recording unless in replay */
  307.         if(midi_flag) load_and_play_midi();
  308.     for (;;) {
  309.         if (rscreen(levelnum,&maxmoves)) {
  310.         strcpy(howdead,"a non-existant screen");
  311.                 levelnum=1;
  312.     //    break;
  313.         }
  314.         dead = playscreen(&levelnum,&score,maxmoves,keys);
  315.  
  316.         if ((dead != NULL) && (*dead == '~'))  /* retry or do level num */
  317.             {
  318.         levelnum = (int)(dead[1]) - 1;
  319.         dead = NULL;
  320.         }
  321.         if (dead != NULL) {     /* player died or wished to quit */
  322.         strcpy(howdead,dead);
  323.         if(alert_kill_status(howdead) == 1) break;
  324.         else {levelnum--; score = old_score;}
  325.                   /* decrement because it will be incremented below */
  326.         }
  327.         levelnum++; /* carry on with this level */
  328.             gamelevel = levelnum;
  329.     }
  330. /* END OF MAIN PROGRAM */
  331.  
  332.     write_ini();
  333.     ier = set_gfx_mode (GFX_TEXT, 640, 480, 0, 0);
  334.     
  335.     printf("WANDERER (C) 1988 S. Shipway. DJGPP MSDOS version by S.Shlien (1997).\n");
  336. //    if (record_file != -1) close(record_file);
  337. }
  338.  
  339.